<?php
namespace Admin\Controller;

use Think\Controller;

class PublicOperateController extends Controller
{

    public function changePwd()
    {
        sess();
        $password = I('post.password');
        if (I('post.password') != I('post.repassword')) {
            $this->ajaxReturn(array(
                zt => false,
                mes => '两次输入密码不一致！'
            ));
        }
        $oldpassword = md5(md5((I('post.oldpassword'))));
        $id = session('uid');
        $User = M("User");
        if ($User->getFieldById($id, 'password') != $oldpassword) {
            $this->ajaxReturn(array(
                zt => false,
                mes => '旧密码错误'
            ));
        } else {
            if (empty($password) || strlen($password) < 5) {
                $this->ajaxReturn(array(
                    zt => false,
                    mes => '新密码长度必须大于或等于6个字符！'
                ));
            } else {
                $User->id = $id;
                $User->password = md5(md5(($password)));
                if ($User->save()) {
                    $this->ajaxReturn(array(
                        zt => true,
                        mes => '密码修改成功！',
                        url => U('Main/main')
                    ));
                } else {
                    $this->ajaxReturn(array(
                        zt => false,
                        mes => '密码修改失败！'
                    ));
                }
                updateTime();
            }
        }
    }

    public function changeUser()
    {
        sess();
        $cu = M('user')->save(I('post.'));
        if ($cu) {
            $this->ajaxReturn(array(
                zt => true,
                mes => '保存成功',
                url => U('Main/main')
            ));
        } else {
            $this->ajaxReturn(array(
                zt => false,
                mes => '保存失败'
            ));
        }
        updateTime();
    }

    public function onlineUser()
    {
        sess();
        $info = M('user');
        $where['update_time'] = array(
            'gt',
            time() - 600
        );
        $count = $info->where($where)->count(); // 查询满足要求的总记录数 $map表示查询条件
        $Page = getpage($count, 5); // 实例化分页类 传入总记录数
        
        $show = $Page->show(); // 分页显示输出
        
        $list = $info->where($where)
            ->limit($Page->firstRow . ',' . $Page->listRows)
            ->select();
        $this->assign('list', $list); // 赋值数据集
        $this->assign('page', $show); // 赋值分页输出
        $this->display();
    }

    public function bian()
    {
        if (IS_POST) {
            $data['id'] = session('uid');
            $data['bian'] = I('post.bian');
            $data['update_time'] = time();
            $bian = M('user')->save($data);
            if ($bian) {
                $this->ajaxReturn(array(
                    zt => true,
                    mes => '保存成功',
                    url => U('Main/main')
                ));
            } else {
                $this->ajaxReturn(array(
                    zt => true,
                    mes => '保存失败'
                ));
            }
        }
    }
}